package com.wanmait.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wanmait.schedule.entity.Userinfo;
import com.wanmait.schedule.mapper.UserinfoMapper;
import com.wanmait.schedule.service.UserinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-03-02
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements UserinfoService {

    @Resource
    private UserinfoMapper userinfoMapper;

    @Override
    public Userinfo login(Userinfo userinfo) {
        LambdaQueryWrapper<Userinfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Userinfo::getAccount,userinfo.getAccount());
        Userinfo loginUser = this.getOne(queryWrapper);
        if(loginUser != null){
            String password = userinfo.getPassword();
            if(password.equals(loginUser.getPassword())){

                return loginUser;
            }
        }
        return null;
    }

    @Override
    public Userinfo isExist(Userinfo userinfo) {
        LambdaQueryWrapper<Userinfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Userinfo::getAccount,userinfo.getAccount());
        Userinfo existUser = this.getOne(queryWrapper);
        if(existUser != null){
            return existUser;
        }
        return null;
    }

    @Override
    public Userinfo updateUserinfoById(Userinfo userinfo) {
        if(userinfo.getId() != null){
            LambdaQueryWrapper<Userinfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Userinfo::getId,userinfo.getId());
            userinfoMapper.update(userinfo,queryWrapper);
            return userinfo;
        }
        return null;
    }


}
